/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.ui;

import ac.ui.controls.Item;
import ac.ui.controls.PCLList;
import ac.ui.utils;
import ac.uitls.propertiesOperation;
import java.awt.event.MouseAdapter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ismail
 */
public class custemItmes {

    ac.lib.database.operations db = new ac.lib.database.operations();
    propertiesOperation po = new propertiesOperation();

    public void comBoxLangs(JComboBox com) {

        ac.uitls.xml x = new ac.uitls.xml();
        String[][] Lang = x.getLanguages();
        comBoxFill(com, Lang[0], Lang[1]);
    }

  
    public static String comBoxGetValue(JComboBox com) {

        String returnData = "";
        try {
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) com.getSelectedItem();
            returnData = itemCate.getValue();
        } catch (Exception ex) {
        }

        return returnData;
    }

    public static String comBoxGetText(JComboBox com) {

        String returnData = "";
        try {
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) com.getSelectedItem();
            returnData = itemCate.getText();
        } catch (Exception ex) {
        }

        return returnData;
    }

    public static void comBoxSetSelectonByValue(JComboBox com, String value) {

        for (int i = 0; i < com.getItemCount(); i++) {
            if (value != null) {
                ac.ui.controls.Item itemCate = (ac.ui.controls.Item) com.getItemAt(i);
///if (!value.equals("")){
                if (itemCate.getValue().equals(value)) {
                    com.setSelectedIndex(i);
                }
            }
            ///  }

        }

    }

    public void comBoxFill(JComboBox com, String table, String valueFiled, String TextFilde) {
        Vector model = new Vector();
        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(table);
        try {
            while (rs.next()) {
                model.addElement(new Item(rs.getString(valueFiled), rs.getString(TextFilde)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(custemItmes.class.getName()).log(Level.SEVERE, null, ex);
        }
        com.setModel(new DefaultComboBoxModel(model));
        db.setCloseCon();
    }

    public void comBoxFill(JComboBox com, String[] data) {
        Vector model = new Vector();
        for (String d : data) {
            model.addElement(new Item(d, d));
        }
        com.setModel(new DefaultComboBoxModel(model));

    }

    public void comBoxFill(JComboBox com, String[] value, String[] text) {
        Vector model = new Vector();
        for (int i = 0; i < text.length; i++) {
            model.addElement(new Item(value[i], text[i]));
        }
        com.setModel(new DefaultComboBoxModel(model));
    }

    public static void comBoxFills(JComboBox com, String[] value, String[] text) {
        Vector model = new Vector();
        for (int i = 0; i < text.length; i++) {
            model.addElement(new Item(value[i], text[i]));
        }
        com.setModel(new DefaultComboBoxModel(model));
    }

    public void comBoxappend(ac.ui.controls.PCLGetdata com, String[] value, String[] text) {
        for (int i = 0; i < text.length; i++) {
            //   com.model.addElement(new Item(value[i], text[i]));

            com.appendValue(value[i], text[i]);

        }
        com.repaint();
    }

    public static void comBoxappends(ac.ui.controls.PCLGetdata com, String[] value, String[] text) {
        for (int i = 0; i < text.length; i++) {

            com.appendValue(value[i], text[i]);

        }
        //    com.repaint();
    }

//</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="List">
    

    public static void listSetSelectonByValue(JList com, String values) {

        String[] vs = values.split(";");

        int[] indexs = new int[vs.length];

        int arr = 0;
        for (String value : vs) {

            for (int i = 0; i < com.getModel().getSize(); i++) {
                if (value != null) {
                    ac.ui.controls.Item itemCate = (ac.ui.controls.Item) com.getModel().getElementAt(i);

                    if (itemCate.getValue().equals(value)) {

                        indexs[arr] = i;
                        arr++;
                    }
                }

            }

            com.setSelectedIndices(indexs);

        }
    }

    public static void listFills(PCLList com, String table, String valueFiled, String TextFilde) {
        Vector model = new Vector();
        ac.lib.database.operations db = new ac.lib.database.operations();
        propertiesOperation po = new propertiesOperation();
        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(table);
        try {
            while (rs.next()) {

                model.addElement(new Item(rs.getString(valueFiled), po.getWord("label." + rs.getString(TextFilde))));

            }
        } catch (SQLException ex) {
            Logger.getLogger(custemItmes.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
        com.setModel(new DefaultComboBoxModel(model));
        db.setCloseCon();
    }

    public static void listFills(JList com, String[] value, String[] text) {
        Vector model = new Vector();
        for (int i = 0; i < text.length; i++) {
            model.addElement(new Item(value[i], text[i]));
        }
        com.setModel(new DefaultComboBoxModel(model));
    }

    public void listFill(JList com, String table, String valueFiled, String TextFilde) {
        Vector model = new Vector();
        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(table);
        try {
            while (rs.next()) {
                model.addElement(new Item(rs.getString(valueFiled), rs.getString(TextFilde)));

            }
        } catch (SQLException ex) {
            Logger.getLogger(custemItmes.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
        com.setModel(new DefaultComboBoxModel(model));
        db.setCloseCon();
    }

    public void listFill(JList com, String[] data) {
        Vector model = new Vector();
        for (String d : data) {
            model.addElement(new Item(d, d));
        }
        com.setModel(new DefaultComboBoxModel(model));

    }

    public void listFill(JList com, String[] value, String[] text) {
        Vector model = new Vector();
        for (int i = 0; i < text.length; i++) {
            model.addElement(new Item(value[i], text[i]));
        }
        com.setModel(new DefaultComboBoxModel(model));
    }

//</editor-fold>
    public void TabelUpdat(JTable jTable1, ResultSet rs, String[] cols, String[] Rows, int[] hideCols) {
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.setRowCount(0);
        model.setColumnIdentifiers(cols);

        //  uiUtil.sendMessage(jTable1, "", null);
        try {

            while (rs.next()) {
                Object[] n = new Object[Rows.length];
                for (int i = 0; i < Rows.length; i++) {
                    n[i] = rs.getString(Rows[i]);
                }
                model.addRow(n);
            }
        } catch (SQLException ex) {
            utils.sendMessage(null, po.getWord("title.error"), ex.getMessage());
        }

        jTable1.setModel(model);
        model.fireTableDataChanged();
        jTable1.repaint();
        // revalidate();
        for (int i = 0; i < hideCols.length; i++) {
            jTable1.getColumnModel().getColumn(hideCols[i]).setMinWidth(0);
            jTable1.getColumnModel().getColumn(hideCols[i]).setMaxWidth(0);
            jTable1.getColumnModel().getColumn(hideCols[i]).setPreferredWidth(0);
        }
    }

    public void fahres(final JTable jtable, final JLabel tableLabelData, JButton frist, JButton perv, JButton next, JButton last) {

        jtable.getSelectionModel().setSelectionInterval(0, 0);
        tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());
        frist.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {

                jtable.getSelectionModel().setSelectionInterval(0, 0);
                tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());
                // jTable1.
            }
        });

        last.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtable.getSelectionModel().setSelectionInterval(jtable.getRowCount() - 1, jtable.getRowCount() - 1);
                tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());
                // jTable1.
            }
        });

        next.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {

                int sel = jtable.getSelectedRow();
                if (sel == jtable.getRowCount() - 1) {
                    jtable.getSelectionModel().setSelectionInterval(0, 0);
                } else if (sel == -1) {
                    jtable.getSelectionModel().setSelectionInterval(jtable.getRowCount() - 1, jtable.getRowCount() - 1);
                } else {

                    jtable.getSelectionModel().setSelectionInterval(sel + 1, sel + 1);

                }

                tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());
                // jTable1.
            }
        });

        perv.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                int sel = jtable.getSelectedRow();
                if (sel == 0) {
                    jtable.getSelectionModel().setSelectionInterval(jtable.getRowCount() - 1, jtable.getRowCount() - 1);
                } else if (sel == -1) {
                    jtable.getSelectionModel().setSelectionInterval(0, 0);
                } else {

                    jtable.getSelectionModel().setSelectionInterval(sel - 1, sel - 1);

                }
                tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());

            }
        });

    }
}
